package vn.sunnet.backviet01.game.coloa.main;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.view.RenderSurfaceView;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

public abstract class LayoutGameAcitivy extends BaseGameActivity{

	protected BitmapTextureAtlas mFontAccountBitmap;
	protected BitmapTextureAtlas mFontVietToBitmap;
	protected BitmapTextureAtlas mFontVietBitmap;
	

	protected Font mFontViet;
	protected Font mFontVietTo;
	protected Font mFontViet1;
	
	protected Handler gameHandler;
	protected boolean loadFinish = false;
	
	private final static int TOTAL_TIME = 6000;
	private long startLoad;
	
	private ProgressBar mLoadProgress;
	private LinearLayout mLoadFrameLayout;
//	protected boolean isViewLoadFinish = false;
	
	protected void onLoadResourcesGame() {
		
		if (loadFinish)
			return;
		
		startLoad = System.currentTimeMillis();
		Thread loading = new Thread(new Runnable() {
			boolean running = true;

			@Override
			public void run() {
				while (running) {
					long currentLoad = System.currentTimeMillis();
					
					if (currentLoad - startLoad > TOTAL_TIME) {
						running = false;
					} else if (loadFinish) {
						mLoadProgress.setProgress(TOTAL_TIME);
					} else
						mLoadProgress.setProgress((int) ((currentLoad - startLoad)));
					
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		loading.start();
	}
	
	protected abstract int getLayoutID();
	protected abstract int getRenderSurfaceViewID();

	@Override
	protected void onSetContentView() {
		super.setContentView(this.getLayoutID());

		this.mRenderSurfaceView = (RenderSurfaceView) this.findViewById(this.getRenderSurfaceViewID());

		this.mRenderSurfaceView.setEGLConfigChooser(false);
		this.mRenderSurfaceView.setRenderer(this.mEngine);
	}
	
	public void onLoadResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		
		mFontAccountBitmap = new BitmapTextureAtlas(256, 256,
				TextureOptions.NEAREST_PREMULTIPLYALPHA);
		mFontVietToBitmap = new BitmapTextureAtlas(512, 512,
				TextureOptions.NEAREST_PREMULTIPLYALPHA);
		mFontVietBitmap = new BitmapTextureAtlas(512, 512, TextureOptions.NEAREST_PREMULTIPLYALPHA);
		
		mFontViet1 = FontFactory.createFromAsset(mFontVietBitmap, this,
				"font/victoria.ttf", 25, true, Color.WHITE);
		mFontViet = FontFactory.createFromAsset(mFontAccountBitmap, this,
				"font/victoria.ttf", 25, true, Color.WHITE);
		mFontVietTo = FontFactory.createFromAsset(mFontVietToBitmap, this,
				"font/victoria.ttf", 35, true, Color.WHITE);

		mEngine.getFontManager().loadFont(mFontVietTo);
		mEngine.getTextureManager().loadTexture(mFontVietToBitmap);
		mEngine.getFontManager().loadFont(mFontViet1);
		mEngine.getTextureManager().loadTexture(mFontVietBitmap);
		mEngine.getFontManager().loadFont(mFontViet);
		mEngine.getTextureManager().loadTexture(mFontAccountBitmap);
	}
	
	
	public Scene onLoadScene() {
		final Scene loadScene = new Scene();
		
		mLoadProgress = (ProgressBar) findViewById(R.id.id_progress_loading);
		mLoadFrameLayout = (LinearLayout) findViewById(R.id.id_loading_layout);

		gameHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case 1:
					mLoadFrameLayout.setVisibility(View.GONE);
					break;

				default:
					break;
				}
			}
		};
		
		IAsyncCallback callback = new IAsyncCallback() {
			
			@Override
			public void workToDo() {
				assetsToLoad();
				
			}
			
			@Override
			public void onComplete() {
				onAssetsLoaded();
				
			}
		};
		
		new AsyncTaskLoader().execute(callback);
		onLoadResourcesGame();
		
		return loadScene;
	}
	
	protected abstract void assetsToLoad();
	protected abstract void onAssetsLoaded();
	
}
